package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 朱安东
 * @version 1.0
 * @project leadnews-project
 * @date 2024/1/25 19:45:10
 */
@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService{
    /**
     * 新增敏感词
     *
     * @param wmSensitive WM 敏感
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult insert(WmSensitive wmSensitive) {
        // 判断前端穿递过来的敏感词是否为空
        if(wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 判断敏感词是否存在 存在不能新增
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if(sensitive != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");
        }
        // 设置修改时间
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 敏感词分页查询
     *
     * @param sensitiveDto 灵敏DTO
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult PageQuery(SensitiveDto sensitiveDto) {
        // 判断数据是否为空
        if(sensitiveDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 进行分页查询
        Page page = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        // 按照创建时间进行倒叙查询
        // 按照敏感词名称进行模糊查询
        wrapper.like(sensitiveDto.getName() != null, WmSensitive::getSensitives, sensitiveDto.getName())
                .orderByDesc(WmSensitive::getCreatedTime);
        page = page(page, wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 修改敏感词
     *
     * @param wmSensitive WM 敏感
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult update(WmSensitive wmSensitive) {
        // 判断参数是否为空
        if(wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 修改
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     *
     * @param id 编号
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        // 检查id是否为空
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 判断敏感词是否存在
        WmSensitive sensitive = getById(id);
        if(sensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不存在");
        }
        // 删除敏感词
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
